package com.gaff.emp.core.config;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.RetryNTimes;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnProperty(prefix = "zookeeper", name = "enable", havingValue = "true")
public class ZookeeperConfig {

    @Value("${zookeeper.retry-count:5}")
    private int retryCount;

    @Value("${zookeeper.elapsed-time:5000}")
    private int elapsedTimeMs;

    @Value("${zookeeper.servers:localhost:2181}")
    private String servers;

    @Value("${zookeeper.session-timeout:60000}")
    private int sessionTimeoutMs;

    @Value("${zookeeper.connect-timeout:5000}")
    private int connectTimeoutMs;

    @Bean
    public CuratorFramework curatorFramework() {
        CuratorFramework client = CuratorFrameworkFactory.newClient(servers, sessionTimeoutMs, connectTimeoutMs, new RetryNTimes(retryCount, elapsedTimeMs));
        client.start();
        return client;
    }

}
